package org.lanqiao.oa.controller;

import com.baomidou.mybatisplus.plugins.Page;
import org.apache.ibatis.annotations.Param;
import org.lanqiao.oa.dto.PermissionDTO;
import org.lanqiao.oa.mapper.ResultInfo;
import org.lanqiao.oa.mapper.Role;
import org.lanqiao.oa.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 跳转页面
     * @param pagePath
     * @return
     */
    @RequestMapping("/forwordPage")
    public String forwordPage(String pagePath, ModelMap modelMap){
        return "forward:/WEB-INF/qx/"+pagePath;
    }

    /**
     * 获取角色跳转查看明细页面
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("/geRole")
    public String geRole(String id, ModelMap modelMap){
        Role role = roleService.selectById(id);
        modelMap.put("role",role);
        return "forward:/WEB-INF/qx/role/detail.jsp";
    }
    /**
     * 获取角色跳转分配权限页面
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("/geRoleToAssign")
    public String geRoleToAssign(String id, ModelMap modelMap){
        Role role = roleService.selectById(id);
        modelMap.put("role",role);
        return "forward:/WEB-INF/qx/role/assign.jsp";
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequestMapping("/addRole")
    @ResponseBody
    public ResultInfo addRole(Role role){
        return ResultInfo.getResultInfo(roleService.addRole(role),"添加成功！！");
    }

    /**
     * 分页查询角色
     * @param page
     * @return
     */
    @RequestMapping("/getPage")
    @ResponseBody
    public Page<Role> getPage(Page page){
        Page<Role> page1 = roleService.selectPage(page);
        return page1;
    }

    /**
     * 删除角色
     * @param ids
     * @return
     */
    @RequestMapping("/deleteRole")
    @ResponseBody
    public ResultInfo deleteRole(@RequestParam("ids[]") String[] ids){
        return ResultInfo.getResultInfo(roleService.deleteRole(ids));
    }

    /**
     * 给角色分配权限
     * @param permissionIds
     * @param roleId
     * @return
     */
    @RequestMapping("/assignPermission")
    @ResponseBody
    public ResultInfo assignPermission(@RequestParam("permission_ids[]") String[] permissionIds,
                                       @RequestParam("role_id") String roleId){
        return ResultInfo.getResultInfo(roleService.assignPermission(permissionIds, roleId));
    }

}
